package org.mcjug.locationmailer.activity;

import java.io.InputStream;

import org.mcjug.locationmailer.R;
import org.mcjug.locationmailer.oauth.tokens.OauthActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.Bundle;
//import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class SignInActivity extends BaseActivity {

	private static final String TAG = SignInActivity.class.getSimpleName();
	
	private Button signInButton;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signin);
        
        signInButton = (Button)findViewById(R.id.signInButton);
        
        signInButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//start oauth process
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), OauthActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
				
				finish();
			}
		});
        
        LinearLayout layout = (LinearLayout)findViewById(R.id.signinLayout);
        layout.addView(new AnimatedGifView(this));
    }
    

	@Override
	protected void onResume() {
		super.onResume();
		checkNetworkConnection();
	}
	
	public class AnimatedGifView extends View {

		Movie movie;
		InputStream is;
		long moviestart;
		
		public AnimatedGifView(Context context) {
			super(context);
		
			is = context.getResources().openRawResource(R.drawable.flying_pony);
			movie = Movie.decodeStream(is);

		}

		@Override
		protected void onDraw(Canvas canvas) {

			super.onDraw(canvas);
			long now = android.os.SystemClock.uptimeMillis();
			
			//Log.d(TAG, "now="+now);
			
			if (moviestart == 0) { // first time
				moviestart = now;
			}

			//Log.d(TAG, "\tmoviestart="+moviestart);
			
			int relTime = (int)((now - moviestart) % movie.duration()) ;
			//Log.d(TAG, "time="+relTime+"\treltime="+movie.duration());
			
			movie.setTime(relTime);
			movie.draw(canvas,this.getWidth()/2-20,this.getHeight()/2-40);
			this.invalidate();
		}
	}
}
